<template>
	<view class="searchLayout">
		<view class="search">
			<uni-search-bar @confirm="onSearch" @cancel="onClear" @clear="onClear" focus placeholder="搜索"
				v-model="queryParams.keyword">
			</uni-search-bar>
		</view>

		<!-- 没有数组长度的时候显示搜索记录 -->
		<view v-if="!classList.length">
			<!-- 如果删除最近搜索记录时候，有长度就显示，没有就不显示 -->
			<view class="history" v-if="historySearch.length">
				<view class="topTitle">
					<view class="text">最近搜索</view>
					<view class="icon" @click="removeHistory">
						<uni-icons type="trash" size="25"></uni-icons>
					</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in historySearch" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>

			<view class="recommend">
				<view class="topTitle">
					<view class="text">热门搜索</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in recommendList" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>
		</view>

		<!-- noSearch的值是true，搜索为空，没有数据， -->
		<view class="noSearch" v-if="noSearch">
			<uv-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png"></uv-empty>
		</view>


		<view v-else>
			<view class="list">
				<navigator :url="`/pages/theme/theme?id=${item._id}`" class="item" v-for="item in classList"
					:key="item._id">
					<image :src="item.smallPicurl" mode="aspectFill"></image>
				</navigator>
			</view>
			<!-- nodata为true说明无数据，在三元运算符里面nodata？后面是true：false -->
			<view class="openLoading" v-if="noData || classList.length"><uni-load-more
					:status="noData?'noMore':'loading'" /></view>
		</view>


	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		onLoad,
		onUnload,
		onReachBottom
	} from "@dcloudio/uni-app";

	//查询参数
	const queryParams = ref({
		pageNum: 1,
		pageSize: 12,
		keyword: ""
	})

	//搜索历史词
	const historySearch = ref(uni.getStorageSync('historySearch') || []);

	//热门搜索词
	const recommendList = ref(["美女", "帅哥", "宠物", "卡通"]);

	//没有更多
	const noData = ref(false);
	//没有搜索结果
	const noSearch = ref(false);
	//搜索结果列表,从接口拿到数据
	const classList = ref([]);

	//点击搜索
	const onSearch = () => {
		console.log(queryParams.value.keyword);
		// new set() 去除重复的搜索,把历史数据和搜索的数据放在一起,只显示10条历史搜索数据
		historySearch.value = [...new Set([queryParams.value.keyword, ...historySearch.value])].splice(0,10)
		//设置把数据放在缓存中
		uni.setStorageSync('historySearch', historySearch.value)
		//搜索之前初始化数据，把搜索的关键词传过来
		initQuery(queryParams.value.keyword)
		//搜索之后调用接口，返回数据
		getSearch()
	}

	//点击清除按钮
	const onClear = () => {
		//点击清除时候对数据初始化
		initQuery()
	}
	//初始化数据
	const initQuery = (value='') => {
		classList.value = []
		noData.value = false
		noSearch.value = false
		queryParams.value = {
			pageNum: 1,
			pageSize: 12,
			keyword: value || ""
		}
	}


	//点击标签进行搜索
	const clickTab = (value) => {
		//点击标签时候初始化数据，实现切换数据
		initQuery(value)
		// value就是热门搜索的标签，也是搜索的关键词
		// queryParams.value.keyword = value
		onSearch()
	}


	//点击清空搜索记录
	const removeHistory = () => {
		uni.showModal({
			title: "是否清空历史搜索？",
			success: res => {
				if (res.confirm) {
					//删除缓存和搜索历史记录
					historySearch.value = []
					uni.removeStorageSync('historySearch')
				}
			}
		})
	}

	//触底加载更多
	onReachBottom(() => {
		// 下拉触底时nodata的值时true,没有数据直接返回出去
		if (noData.value) return
		queryParams.value.pageNum++
		getSearch()
	})

	//关闭有页面
	onUnload(() => {
uni.removeStorageSync('storagClasslist',classList.value)
	})
	const getSearch = async () => {
		const res = await uni.request({
			url: "https://tea.qingnian8.com/api/bizhi/searchWall",
			header: {
				"access-key": "238762"
			},
			data: queryParams.value
		})
	
		// 把接口的数据返回给数组calsslist,在对他进行旧值和新值的拼接
		classList.value = [...classList.value, ...res.data.data]
		//将从接口拿到的数据也缓存到本地
		 uni.setStorageSync('storagClasslist',classList.value)	
		// 如果pagesize的值12>接口返回的大小,说明没有数据了,也不会再请求接口了
		if (queryParams.value.pageSize > res.data.data.length) noData.value = true
		// 如果接口数据的长度和数组长度都是0的时候,搜索结果为空
		if (res.data.data.length === 0 && classList.value === 0) noSearch.value = true
		
	}
</script>

<style lang="scss" scoped>
	.searchLayout {
		.search {
			padding: 0 10rpx;
		}

		.topTitle {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 32rpx;
			color: #999;
		}

		.history {
			padding: 30rpx;
		}

		.recommend {
			padding: 30rpx;
		}

		.tabs {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			padding-top: 20rpx;

			.tab {
				background: #F4F4F4;
				font-size: 28rpx;
				color: #333;
				padding: 10rpx 28rpx;
				border-radius: 50rpx;
				margin-right: 20rpx;
				margin-top: 20rpx;
			}
		}

		.list {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 5rpx;
			padding: 20rpx 5rpx;

			.item {
				height: 440rpx;

				image {
					width: 100%;
					height: 100%;
					display: block;
				}
			}
		}
	}
</style>